#include <libpub.h>

#define DELAY_10MS      10
#define DELAY_100MS     100
#define DELAY_1000MS    1000
#define DELAY_2000MS    2000

extern "C"{
void backlight_on();
void backlight_off();
void backlight_setbrightness(int bright_val);
int BackLight_SetBrightness(void *pCxt, int bright_val);
void msleep(unsigned int ms);
int Do_InitLcd(void);
void Display_Logo();
}

int Display_Red_For_Backlight_Test();
int BackLight_SetBrightness(void *pCxt, int bright_val)
{
    TOUT(0, 1, printf("Backlight_SetBrightness(%x)", bright_val));
    backlight_setbrightness(bright_val);
    return 0;
}

void backlight_close()
{
    BackLight_SetBrightness(0, 0);
    backlight_off();
}

int nBrightTable[11][2]={
    {0,  3},  //0
    {1,  3},  //1
    {2,  3},  //2
    {3, 3},  //3
    {4, 3},  //4
    {5, 3},  //5 default
    {5, 3},  //6
    {6, 4},  //7
    {6, 4},  //8
    {7, 4},  //9
    {7, 5},  //10
};

static int Do_Test_Backlight_Auto(void)
{
    TOUT(0, 1, printf("\n Do_Test_LcdBacklight_Auto():\n"));

    //test light close and open
    backlight_close();
    msleep(DELAY_100MS);
    backlight_on();
    msleep(DELAY_100MS);

//    Display_Red_For_Backlight_Test();//Because No Logo Displayed Use this instead
    //test light all level
    int l = 0;
    for(l = 0; l < 11; l++){
        BackLight_SetBrightness(0, nBrightTable[l][0]);
        TOUT(1, 1, printf("\nSet BackLight = %d", nBrightTable[l][0]));
        msleep(DELAY_100MS);
    }

    backlight_on();

    return 0;
}

int _Do_Test_Backlight(char *param)
{
    TOUT(0,1,printf("\n _Do_Test_Backlight(%s):\n", param));

//    Do_InitLcd();
//    Display_Logo();

    int bright_val = 0;

    if (*param) {//test backlight 100
        param = Bo_ParseInteger(param, (unsigned long *) &bright_val);
        TOUT(1, 1, printf("\n Light value = (%d):\n", bright_val));
        BackLight_SetBrightness(0, bright_val);
   }
   else {//test backlight
        Do_Test_Backlight_Auto();
   }
   return 0;
}
